<template>
  <div>
    <van-nav-bar
      title="账号登录"
      left-arrow
      @click-left="onClickLeft"
      class="login"
      >
    
   </van-nav-bar>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
    <router-link to="/register" class='link'>还没有账号，去注册~</router-link>
  </div>
</template>

<script>
import {login} from '@/api/users'
import {setToken} from '@/utils/storage'
export default {
  name: "",
  components: {},
  props: {},
  data() {
    return {
      username: "guojunqing",
      password: "123456",
    };
  },
  methods: {
   async onSubmit(values) {
      console.log("submit", values);
      const data=await login(values)
      if(data.status===200){
       this.$toast.success(data.description)
       setToken(data.body.token)
       this.$router.push('/home')
      }else{
        this.$toast.success(data.description)
      }
    },
    onClickLeft() {
      this.$router.go(-1)
    },
  },
};
</script>

<style scoped lang='less'>
.login{
background-color:#1cb676;
color: #fff;

}
.link{
  color:#ccc;
}
</style>
